package org.cagui.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.cagui.core.CertificateManager;
import org.cagui.core.CertificateParams;
import org.cagui.core.CsrManager;
import org.cagui.core.KeyStoreManager;

public class CsrReq extends JFrame implements ActionListener{

	private static final long serialVersionUID = -5902492963495979446L;
	private JButton bgen, bcan;
	private JLabel gui, genrsakeybitlong, country,
			stateorprovincename, localityname, organizationname,
			organizationunit, commonname, emailaddress, achellengpassword,
			companyname2, ca, chooseKeystore, validityLabel;

	private JTextField tcountry,
			tstateorprovincename, tlocalityname, torganizationname,
			torganizationunit, tcommonname, temailaddress, tachellengpassword,
			tcompanyname2, tca, validity;
	
	private JComboBox tgenrsakeybitlong, keystore;
	private String tempGen, tempDat, tempGlong, tempCoun, tempSta, tempLoc,
			tempOrg, tempOrgU;
	private String tempComm, tempEmail, tempChell, tempCom, tempTca, tempKeyStoreName, tempValidity;

	private JLabel filename, keyStorePass;
	private JTextField filenameTextField, keyStorePassTextField;
	
	public CsrReq() {
		ArrayList<JLabel> labels = new ArrayList<JLabel>();
		ArrayList<JComponent> textFields = new ArrayList<JComponent>();
		setPreferredSize(new Dimension(570, 500));
		setTitle("NEW CSR");

		// Nag��wek
		gui = new JLabel("NEW CSR", JLabel.CENTER);
		gui.setForeground(Color.ORANGE);
		gui.setFont(new Font("Calibri", Font.PLAIN, 25));

		// Code Autoryzation
		ca = new JLabel("Code Autoryzation:");
		//labels.add(ca);

		tca = new JTextField("");
		//textFields.add(tca);
		
		// Gen.RSA.Key.bit.long

		genrsakeybitlong = new JLabel("Gen.RSA.Key.bit.long:");
		labels.add(genrsakeybitlong);

		tgenrsakeybitlong = new JComboBox(new Integer[] { 1024, 2048 });
		textFields.add(tgenrsakeybitlong);

		// Country Name
		country = new JLabel("Country Name:");
		labels.add(country);

		tcountry = new JTextField("");
		textFields.add(tcountry);

		// State/Province name
		stateorprovincename = new JLabel("State/Province name:");
		labels.add(stateorprovincename);

		tstateorprovincename = new JTextField("");
		textFields.add(tstateorprovincename);

		// Locality Name
		localityname = new JLabel("Locality Name:");
		labels.add(localityname);

		tlocalityname = new JTextField("");
		textFields.add(tlocalityname);

		// Organizatio Name
		organizationname = new JLabel("Organizatio Name:");
		labels.add(organizationname);

		torganizationname = new JTextField("");
		textFields.add(torganizationname);

		// Organization Unit
		organizationunit = new JLabel("Organization Unit:");
		labels.add(organizationunit);

		torganizationunit = new JTextField("");
		textFields.add(torganizationunit);

		// Common Name
		commonname = new JLabel("Common Name:");
		labels.add(commonname);

		tcommonname = new JTextField("");
		textFields.add(tcommonname);

		// Email Address
		emailaddress = new JLabel("Email Address:");
		labels.add(emailaddress);

		temailaddress = new JTextField("");
		textFields.add(temailaddress);

		// *A Chelleng Password
		achellengpassword = new JLabel("*A Chelleng Password:");
		labels.add(achellengpassword);

		tachellengpassword = new JTextField("");
		textFields.add(tachellengpassword);

		// *Company Name
		companyname2 = new JLabel("*Company Name:");
		labels.add(companyname2);

		tcompanyname2 = new JTextField("");
		textFields.add(tcompanyname2);

		// Validity - waznosc w godzinach
		validityLabel = new JLabel("Validity: [h]");
		//labels.add(validityLabel);

		validity = new JTextField("");
		//textFields.add(validity);

		// *File name
		filename = new JLabel("File name to save csr:");
		labels.add(filename);

		filenameTextField = new JTextField("");
		textFields.add(filenameTextField);

		// CHose keystore
		chooseKeystore = new JLabel("Chose keystore");
		//labels.add(chooseKeystore);

		KeyStoreManager keystoreManager = new KeyStoreManager();

		keystore = new JComboBox(keystoreManager.getKeystoresNames());
		//textFields.add(keystore);

		// *KeyStore Pass
		keyStorePass = new JLabel("KeyStore password:");
		//labels.add(keyStorePass);

		keyStorePassTextField = new JTextField("");
		//textFields.add(keyStorePassTextField);

		// Przycisk GENERATE
		bgen = new JButton("GENERATE");
		bgen.addActionListener(this);

		// Przycisk EXIT
		// bcan = new JButton("EXIT");
		// bcan.addActionListener(this);

		BorderLayout borderLayout = new BorderLayout();
		this.setLayout(borderLayout);
		add(gui, BorderLayout.NORTH);

		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridLayout(labels.size(), 2));
		for (int i = 0; i < labels.size(); i++) {
			labels.get(i).setForeground(Color.BLACK);
			labels.get(i).setFont(new Font("Calibri", Font.PLAIN, 17));
			centerPanel.add(labels.get(i));
			centerPanel.add(textFields.get(i));

		}

		centerPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
		add(centerPanel, BorderLayout.CENTER);

		JPanel southPanel = new JPanel();
		add(southPanel, BorderLayout.SOUTH);
		southPanel.setLayout(new GridLayout(1, 2));
		southPanel.add(bgen);
		southPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
		// southPanel.add(bcan);
		pack();
		setVisible(true);

	}

	/*
	 * public static void main(String[] args) { NEWCA GUI = new NEWCA();
	 * GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GUI.setVisible(true);
	 * }
	 */

	@Override
	public void actionPerformed(ActionEvent e) {

		tempGen = ((Integer) tgenrsakeybitlong.getSelectedItem()).toString();

		// tempDat = tdata.getText();

		tempCoun = tcountry.getText();

		tempSta = tstateorprovincename.getText();

		tempLoc = tlocalityname.getText();

		tempOrg = torganizationname.getText();

		tempOrgU = torganizationunit.getText();

		tempComm = tcommonname.getText();

		tempEmail = temailaddress.getText();

		tempChell = tachellengpassword.getText();

		tempCom = tcompanyname2.getText();

		//tempTca = tca.getText();

		//tempKeyStoreName = keystore.getSelectedItem().toString();

		//tempValidity = validity.getText();

		Object z = e.getSource();
		if (z == bgen
				//&& !tempGen.equals("")
				&&
				// tempDat!="" &&
				!tempCoun.equals("") && !tempSta.equals("")
				&& !tempLoc.equals("") && !tempOrg.equals("")
				&& !tempOrgU.equals("") && !tempComm.equals("")
				&& !tempEmail.equals("") 
				&& !filenameTextField.getText().equals("")
				
		)

		{

			CertificateParams params = new CertificateParams();

			params.setRsaKeyLength(Integer.parseInt(tempGen));
			params.setAlias(tempComm);
			params.setCountry(tempCoun);
			params.setState(tempSta);
			params.setOrganizationName(tempOrg);
			params.setOrganizationUnit(tempOrgU);
			params.setFileNameForCertificate(filenameTextField.getText());

			try {
				CsrManager cm = new CsrManager();
				cm.generateCertificationRequest(params);
			} catch (Exception exception) {
				System.out.println(exception.getMessage());
			}

			dispose();

		} else if (z == bgen) {

			JOptionPane.showMessageDialog(this, "All fields have to be set!");

		}

		if (z == bcan) {
			dispose();
		}

	}

	public String getTempGen() {
		return tempGen;
	}

	public void setTempGen(String tempGen) {
		this.tempGen = tempGen;
	}

	public String getTempDat() {
		return tempDat;
	}

	public void setTempDat(String tempDat) {
		this.tempDat = tempDat;
	}

	public String getTempGlong() {
		return tempGlong;
	}

	public void setTempGlong(String tempGlong) {
		this.tempGlong = tempGlong;
	}

	public String getTempCoun() {
		return tempCoun;
	}

	public void setTempCoun(String tempCoun) {
		this.tempCoun = tempCoun;
	}

	public String getTempSta() {
		return tempSta;
	}

	public void setTempSta(String tempSta) {
		this.tempSta = tempSta;
	}

	public String getTempLoc() {
		return tempLoc;
	}

	public void setTempLoc(String tempLoc) {
		this.tempLoc = tempLoc;
	}

	public String getTempOrg() {
		return tempOrg;
	}

	public void setTempOrg(String tempOrg) {
		this.tempOrg = tempOrg;
	}

	public String getTempOrgU() {
		return tempOrgU;
	}

	public void setTempOrgU(String tempOrgU) {
		this.tempOrgU = tempOrgU;
	}

	public String getTempComm() {
		return tempComm;
	}

	public void setTempComm(String tempComm) {
		this.tempComm = tempComm;
	}

	public String getTempEmail() {
		return tempEmail;
	}

	public void setTempEmail(String tempEmail) {
		this.tempEmail = tempEmail;
	}

	public String getTempChell() {
		return tempChell;
	}

	public void setTempChell(String tempChell) {
		this.tempChell = tempChell;
	}

	public String getTempCom() {
		return tempCom;
	}

	public void setTempCom(String tempCom) {
		this.tempCom = tempCom;
	}

	public String getTempTca() {
		return tempTca;
	}

	public void setTempTca(String tempTca) {
		this.tempTca = tempTca;
	}
}
